#include "TestLineEdit.h"
#include<QLineEdit>
#include<QVBoxLayout>
#include<QIcon>
#include<QAction>
#include<QDebug>


TestLineEdit::TestLineEdit(QWidget *parent) : QWidget(parent)
{
    //1.用户登录界面输入，用户名输入框
    QLineEdit *usernameEdit = new QLineEdit;
    usernameEdit->setPlaceholderText("username");// 设置占位符文本
    usernameEdit->setMaxLength(20);//限制输入长度

    // 创建密码输入框
    QLineEdit *passwordEdit = new QLineEdit;
    passwordEdit->setPlaceholderText("密码");
    passwordEdit->setEchoMode(QLineEdit::Password); // 设置回显模式（密码输入）
    passwordEdit->setMaxLength(16);
    // 添加清除按钮
    QAction *action_clear=passwordEdit->addAction(QIcon(":/images/clear.png"), QLineEdit::TrailingPosition);
    action_clear->setToolTip("清除内容");
    connect(action_clear,&QAction::triggered,passwordEdit,&QLineEdit::clear);

    //2 搜索功能实现
    QLineEdit *searchEdit = new QLineEdit;
    searchEdit->setPlaceholderText("搜索...");
    searchEdit->setClearButtonEnabled(true); // 启用内置清除按钮

    // 添加搜索图标
    QAction *searchAction = searchEdit->addAction(QIcon(":/images/search.png"), QLineEdit::LeadingPosition);
    searchAction->setToolTip("Search");

    // 连接回车信号
    connect(searchEdit, &QLineEdit::returnPressed, [=](){
        performSearch(searchEdit->text());
    });
    //3 设置对齐方式、占位文本和清除按钮
    QLineEdit *lineEdit3 = new QLineEdit;
    lineEdit3->setAlignment(Qt::AlignCenter); // 文本居中
    lineEdit3->setPlaceholderText("name");
    lineEdit3->setClearButtonEnabled(true);

    //4 设置密码输入框
    QLineEdit *passwordEdit4 = new QLineEdit;
    passwordEdit4->setEchoMode(QLineEdit::Password);
    passwordEdit4->setMaxLength(16); // 密码最大长度16

    //5 使用输入掩码（日期格式）
    QLineEdit *dateEdit5 = new QLineEdit;
    dateEdit5->setInputMask("0000-00-00");
    dateEdit5->setPlaceholderText("YYYY-MM-DD");

    //6 获取和设置文本
    QLineEdit *lineEdit6 = new QLineEdit;
    lineEdit6->setText("初始文本");
    QString currentText = lineEdit6->text();

    // 连接文本改变信号
    connect(lineEdit6, &QLineEdit::textChanged, [](const QString &text) {
        qDebug() << "文本改变为：" << text;
    });

    //7 示例5：设置只读和边框
    QLineEdit *lineEdit7 = new QLineEdit;
    lineEdit7->setReadOnly(true);
    lineEdit7->setFrame(false); // 不显示边框


    QVBoxLayout *vlayout =new QVBoxLayout(this);
    vlayout->addWidget(usernameEdit);
    vlayout->addWidget(passwordEdit);
    vlayout->addWidget(searchEdit);
    vlayout->addWidget(lineEdit3);
    vlayout->addWidget(dateEdit5);
    vlayout->addWidget(lineEdit6);
    vlayout->addWidget(lineEdit7);
}

void TestLineEdit::performSearch(const QString &txt)
{

}
